package com.base.key.controller;

import java.util.List;
import java.util.Arrays;

import com.base.key.domain.vo.TKeyUseVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.base.common.annotation.RepeatSubmit;
import com.base.common.annotation.Log;
import com.base.common.core.controller.BaseController;
import com.base.common.core.domain.PageQuery;
import com.base.common.core.domain.R;
import com.base.common.core.validate.AddGroup;
import com.base.common.core.validate.EditGroup;
import com.base.common.enums.BusinessType;
import com.base.common.utils.poi.ExcelUtil;
import com.base.key.domain.bo.TKeyUseBo;
import com.base.key.service.ITKeyUseService;
import com.base.common.core.page.TableDataInfo;

/**
 * 密钥在用表
 *
 * @author base
 * @date 2023-10-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/key/keyUse")
public class TKeyUseController extends BaseController {

    private final ITKeyUseService iTKeyUseService;

    /**
     * 查询密钥在用表列表
     */
// @SaCheckPermission("key:keyUse:list")
    @GetMapping("/list")
    public R<TableDataInfo<TKeyUseVo>> list(TKeyUseBo bo, PageQuery pageQuery) {
        return R.ok(iTKeyUseService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出密钥在用表列表
     */
    //   @SaCheckPermission("key:keyUse:export")
    @Log(title = "密钥在用表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TKeyUseBo bo, HttpServletResponse response) {
        List<TKeyUseVo> list = iTKeyUseService.queryList(bo);
        ExcelUtil.exportExcel(list, "密钥在用表", TKeyUseVo.class, response);
    }

    /**
     * 获取密钥在用表详细信息
     *
     * @param keyCode 主键
     */
    //    @SaCheckPermission("key:keyUse:query")
    @GetMapping("/{keyCode}")
    public R<TKeyUseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String keyCode) {
        return R.ok(iTKeyUseService.queryById(keyCode));
    }

    /**
     * 新增密钥在用表
     */
//     @SaCheckPermission("key:keyUse:add")
    @Log(title = "密钥在用表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TKeyUseBo bo) {
        return toAjax(iTKeyUseService.insertByBo(bo));
    }

    /**
     * 修改密钥在用表
     */
    //   @SaCheckPermission("key:keyUse:edit")
    @Log(title = "密钥在用表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TKeyUseBo bo) {
        return toAjax(iTKeyUseService.updateByBo(bo));
    }

    /**
     * 删除密钥在用表
     *
     * @param keyCodes 主键串
     */
    //  @SaCheckPermission("key:keyUse:remove")
    @Log(title = "密钥在用表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{keyCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] keyCodes) {
        return toAjax(iTKeyUseService.deleteWithValidByIds(Arrays.asList(keyCodes), true));
    }
}
